package jpave.query_interface;
/****************************************************
 * The actual query is in FieldMapper method getContigObjFromDB 
 */
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.Color;

import util.ui.ButtonComboBox;

import jpave.dataholders.FilterContigData;

public class NFoldColumnSelectPanel extends JPanel {
	private static final long serialVersionUID = 5636465225630988236L;

	public NFoldColumnSelectPanel(final FieldTab fTab, JCheckBox [] allLibraries) {
		final int THEIGHT = 120;
		final int TWIDTH = 50;
		
		selectedFolds = new Vector<String> ();
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setAlignmentX(Component.LEFT_ALIGNMENT);
		strLibNames = new String[allLibraries.length];
		for(int x=0; x<strLibNames.length; x++) {
			strLibNames[x] = allLibraries[x].getText();
		}
		
		cmbRefLib = new ButtonComboBox();
		cmbLib = new ButtonComboBox();
		
		cmbRefLib.addItem("Select");
		cmbLib.addItem("Select");
		
		for(int x=0; x<strLibNames.length; x++) {
			cmbRefLib.addItem(strLibNames[x]);
			cmbLib.addItem(strLibNames[x]);
		}
		
		cmbRefLib.setSelectedIndex(0);
		cmbLib.setSelectedIndex(0);
		
		cmbRefLib.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnAdd.setEnabled((cmbRefLib.getSelectedIndex() > 0 
						&& cmbLib.getSelectedIndex() > 0 
						&& cmbRefLib.getSelectedIndex() != cmbLib.getSelectedIndex()));
			}
		});
		
		cmbLib.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnAdd.setEnabled((cmbRefLib.getSelectedIndex() > 0 
						&& cmbLib.getSelectedIndex() > 0 
						&& cmbRefLib.getSelectedIndex() != cmbLib.getSelectedIndex()));
			}
		});
		
		btnAdd = new JButton("Add");
		btnAdd.setEnabled(false);
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String newItem = cmbRefLib.getSelectedItem() + "/" + cmbLib.getSelectedItem();
				if(!selectedFolds.contains(newItem)) {
					selectedFolds.add(newItem);
					Vector<String> newRow = new Vector<String>();
					newRow.add(newItem);
					theTableModel.addRow(newRow);
				}
				firstRun = false;
				fTab.updateFields();
			}
		});
		
		String headers[] = {"test"};
		theTableModel = new DefaultTableModel(headers,0);
		theTable = new JTable(theTableModel);
		sPane = new JScrollPane(theTable);
		
		theTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				btnRemove.setEnabled(theTable.getSelectedRowCount() > 0);
			}
		});
		theTable.setTableHeader(null);
		theTable.setAlignmentX(Component.LEFT_ALIGNMENT);
		
		Dimension temp = sPane.getPreferredSize();
		temp.height = THEIGHT;
		temp.width = TWIDTH;
		sPane.setPreferredSize(temp);
		sPane.setAlignmentX(Component.LEFT_ALIGNMENT);

		btnRemove = new JButton("Remove");
		btnRemove.setEnabled(false);
		btnRemove.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				int nrow = theTable.getSelectedRow();
				selectedFolds.remove(nrow);
				theTableModel.removeRow(nrow);
				firstRun = false;
				fTab.updateFields();
			}
		});

		btnAdd.setMinimumSize(btnRemove.getMinimumSize());
		btnAdd.setMaximumSize(btnRemove.getMaximumSize());
		btnAdd.setPreferredSize(btnRemove.getPreferredSize());
			
		add(sPane);
		
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment(JLabel.CENTER);
		theTable.getColumnModel().getColumn(0).setCellRenderer(centerRenderer);
		
		JPanel ctrlPanel = new JPanel();
		ctrlPanel.setLayout(new BoxLayout(ctrlPanel, BoxLayout.LINE_AXIS));
		ctrlPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		
		ctrlPanel.add(cmbRefLib);
		ctrlPanel.add(Box.createHorizontalStrut(10));
		ctrlPanel.add(cmbLib);
		add(Box.createVerticalStrut(5));
		add(ctrlPanel);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(Color.white);
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
		buttonPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		
		buttonPanel.add(btnAdd);
		buttonPanel.add(Box.createHorizontalStrut(10));
		buttonPanel.add(btnRemove);
		
		JPanel rowPanel = new JPanel();
		rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.LINE_AXIS));
		rowPanel.setAlignmentX(Component.LEFT_ALIGNMENT);

		rowPanel.add(Box.createHorizontalGlue());
		rowPanel.add(buttonPanel);
		rowPanel.add(Box.createHorizontalGlue());
		add(Box.createVerticalStrut(10));
		add(rowPanel);	
	}
	
	public String [] getColumnNames() {
		return selectedFolds.toArray(new String[0]);
	}
	
	public int [] getIDs(){	
		int [] retVal = new int[selectedFolds.size()];
		for(int x=0; x<retVal.length; x++)
			retVal[x] = FilterContigData.N_FOLD_LIB + x;
		return retVal;
	}
	
	public int getIDForName(String name) {
		if(!selectedFolds.contains(name))
			return -1;
		return FilterContigData.N_FOLD_LIB + selectedFolds.indexOf(name);
	}

	public void addFoldColumn(String colName) {
		if (!firstRun) return;
		String [] libs  = colName.split("/");
		boolean found = false;
		
		for(int x=0; x<strLibNames.length && !found; x++) {
			if(libs[0].equals(strLibNames[x]))
				found = true;
		}
		
		if(found) {
			found = false;
			for(int x=0; x<strLibNames.length && !found; x++) {
				if(libs[1].equals(strLibNames[x]))
					found = true;
			}
			
			if(found) {
				if(!selectedFolds.contains(colName)) {
					selectedFolds.add(colName);
					theTableModel.addRow(new String[]{colName});
				}
			}
		}
	}
	
	public void addFoldColumns(FieldMapper mapper) {
		String [] cols = getColumnNames();
		int [] ids = getIDs();
		
		int [] allIDs = mapper.getAllFieldIDs();
		
		for(int IDIdx = 0; IDIdx<ids.length; IDIdx++) {
			boolean found = false;
			for(int x=0; x<allIDs.length && !found; x++) {
				if(allIDs[x] == ids[IDIdx]) {
					found = true;
				}
			}
			if(!found) {
				mapper.addFloatField(ids[IDIdx], cols[IDIdx], null, null, null, null, null);
			}
		}
	}
		
	private JScrollPane sPane = null;
	private JTable theTable = null;
	private DefaultTableModel theTableModel = null;
	
	ButtonComboBox cmbRefLib = null;
	ButtonComboBox cmbLib = null;
	
	JButton btnAdd = null, btnRemove = null;

	private String [] strLibNames = null;
	private Vector<String> selectedFolds = null;
	
	private boolean firstRun = true; // WN: a hack to prevent continual re-setting of columns from the preference file
}
